<template>
    <div id="list">
        <!-- 头部刷新区域 -->
        <div class="setdiv">
            <span>退款申请</span>
            <el-button class="RERight" plain icon="el-icon-refresh" @click="Newpage">刷新</el-button>
        </div>
        <el-card>
            <!-- 搜索折叠区 -->
            <el-collapse v-model="activeNames">
                <el-collapse-item title="筛选查询" name="1">
                    <el-form :inline="true" :model="formInline" class="demo-form-inline">
                        <div id="input1">
                            关键词<el-input class="input" v-model="formInline.search" placeholder="订单号/商品货号"></el-input>
                            收货人<el-input class="input" v-model="formInline.phone" placeholder="用户昵称/手机号"></el-input>
                            提交时间<el-select v-model="formInline.select" class="input" slot="prepend" placeholder="请选择时间">
                                <el-option label="2022" value="2022"></el-option>
                                <el-option label="2021" value="2021"></el-option>
                                <el-option label="2020" value="2020"></el-option>
                            </el-select>
                        </div>
                    </el-form>
                    <div id="anniu1">
                        <el-button type="info" plain @click="Reset">重置</el-button>
                        <el-button type="info" plain @click="Screening">筛选</el-button>
                    </div>
                </el-collapse-item>
            </el-collapse>
            <!-- 数据 -->
            <el-card class="box-card Tdata">
                <div slot="header" class="clearfix">
                    <span>数据列表</span>
                    <el-button style="float: right; padding: 3px 0" type="text" @click="batchDelSure">批量删除</el-button>
                    <!-- <el-button style="float: right; padding: 3px 20px" type="text" @click="addData">添加</el-button> -->
                </div>
                <!-- 获取数据渲染表格 -->
                <el-table :data="tableData" border style="width: 100%" @selection-change="selectCheck">
                    <el-table-column type="selection" width="55">
                    </el-table-column>
                    <el-table-column prop="id" label="序号" width="50">
                    </el-table-column>
                    <el-table-column prop="ordernum" label="订单编号" width="150">
                    </el-table-column>
                    <el-table-column prop="subtime" label="申请时间" width="200">
                    </el-table-column>
                    <el-table-column prop="phone" label="用户账号" width="150">
                    </el-table-column>
                    <el-table-column prop="money" label="退款金额" width="150">
                    </el-table-column>
                    <el-table-column prop="type" label="申请状态" width="150">
                    </el-table-column>
                    <el-table-column prop="handletime" label="处理时间" width="200">
                    </el-table-column>
                    <el-table-column label="操作">
                        <template slot-scope="scope">
                            <el-button type="text" @click="handleEdit(scope.row)">编辑</el-button>
                            <el-button type="text" @click="open(scope.row)">删除</el-button>
                        </template>
                    </el-table-column>
                </el-table>
            </el-card>
            <!-- 分页 -->
            <div id="fenye">
                <div class="block">
                    <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" background :page-sizes="[5, 10, 20, 30]" layout="total, sizes, prev, pager, next, jumper" :total="userlist.length" :page-size="eachNum" :current-page.sync="nowPage">
                    </el-pagination>
                </div>
            </div>
            <!-- 编辑退货详情弹框 -->
            <div>
                <el-dialog title="退款申请详情" :visible.sync="CheckDialogVisible" width="50%" :before-close="editClose">
                    <!-- 编辑退货-->
                    <el-form ref="editTableData" :model="editTableData" :rules="rules" label-width="80px">
                        <el-form-item prop="id" label="序号">
                            <el-input v-model="editTableData.id" disabled></el-input>
                        </el-form-item>
                        <el-form-item prop="ordernum" label="订单编号">
                            <el-input v-model="editTableData.ordernum" disabled></el-input>
                        </el-form-item>
                        <el-form-item prop="subtime" label="申请时间">
                            <el-input type="datetime-local" v-model="editTableData.subtime" disabled></el-input>
                        </el-form-item>
                        <el-form-item prop="phone" label="用户账户">
                            <el-input v-model="editTableData.phone"></el-input>
                        </el-form-item>
                        <el-form-item prop="money" label="退款金额" width="150">
                            <el-input v-model="editTableData.money"></el-input>
                        </el-form-item>
                        <el-form-item prop="type" label="申请状态" width="150">
                            <el-input v-model="editTableData.type"></el-input>
                        </el-form-item>
                        <el-form-item prop="handletime" label="处理时间" width="200">
                            <el-input type="datetime-local" v-model="editTableData.handletime"></el-input>
                        </el-form-item>
                    </el-form>
                    <span slot="footer" class="dialog-footer">
                        <el-button @click="editClose">取 消</el-button>
                        <el-button type="primary" @click="onEditOk">确 定</el-button>
                    </span>
                </el-dialog>
            </div>
        </el-card>
    </div>
</template>
<script>
import date from "../../components/package";
export default {
    data() {
        // 验证手机号的规则
        var checkMobile = (rule, value, cd) => {
            const regMobile = /^1[3|4|5|7|8][0-9]{9}$/;
            if (regMobile.test(value)) {
                return cd();
            }
            cd(new Error("请输入合法的手机号"));
        };
        return {
            // 表单的验证
            rules: {
                phone: [
                    {
                        required: true,
                        message: "请输入手机号",
                        trigger: "blur",
                    },
                    {
                        validator: checkMobile,
                        trigger: "blur",
                    },
                ],
            },
            // 搜索内容
            formInline: {
                phone: "",
                select: "",
                search: "",
            },
            // 存所选中的信息
            batchDelFrom: {},
            // 添加数据时的存储
            // addTableData: {
            //     ordernum: "",
            //     subtime: "",
            //     // userid: "",
            //     money: "",
            //     type: "",
            //     paymethod: "",
            // },
            // 添加内容对话框隐藏
            dialogVisible: false,
            // 头部折叠框
            activeNames: ["1"],
            // 当前页
            nowPage: 1,
            // 每页显示五条
            eachNum: 5,
            userlist: [],
            // 查看详情弹框
            CheckDialogVisible: false,
            // 退款详情表一数据
            editTableData: {
                id: "",
                ordernum: "",
                subtime: "",
                phone: "",
                money: "",
                type: "",
            },
        };
    },
    created() {
        this.getUserList();
    },
    methods: {
        // 批量删除
        batchDelSure() {
            this.$confirm("确认删除所选退款信息？", "提示", {
                confirmButtonText: "确定",
                cancelButtonText: "取消",
                type: "warning",
            }).then(() => {
                console.log(this.batchDelFrom);
                let str = "";
                for (let i = 0; i < this.batchDelFrom.length; i++) {
                    if (this.batchDelFrom[i].num) {
                        str = str + `"${this.batchDelFrom[i].name}"` + " ";
                    }
                }
                this.batchDel();
            });
        },
        batchDel() {
            this.$axios({
                url: "/refund_information",
                method: "post",
                data: this.batchDelFrom,
            }).then((res) => {
                if (res.data.code == 200) {
                    this.getUserList();
                    this.$message({
                        message: "删除成功",
                        type: "success",
                    });
                } else {
                    this.$message({
                        message: "失败",
                        type: "warning",
                    });
                }
            });
        },
        selectCheck(selection) {
            this.batchDelFrom = selection;
        },
        // 添加数据时确认信息
        addTableData1() {
            this.$axios({
                url: "/addinformation",
                method: "post",
                data: {
                    ordernum: this.addTableData.ordernum,
                    subtime: date.dateFn(this.addTableData.subtime),
                    money: this.addTableData.money,
                    type: this.addTableData.type,
                    paymethod: date.dateFn(this.addTableData.paymethod),
                },
            }).then((res) => {
                console.log(res);
                if (res.data.code == 200) {
                    this.$message({
                        message: "添加成功",
                        type: "success",
                    });
                }
            });
            this.dialogVisible = false;
            this.getUserList();
        },
        // 添加对话框按钮
        addData() {
            this.dialogVisible = true;
        },
        handleClose(done) {
            this.$confirm("确认关闭？")
                .then((_) => {
                    done();
                })
                .catch((_) => {});
        },
        // 初始化数据列表
        getUserList() {
            // console.log("获取数据");
            this.$axios({
                url: "/orders/getallorders",
                // params: this.queryInfo,
            }).then((res) => {
                res.data.data.filter((v) => {
                    v.subtime = date.dateFn(v.subtime);
                    v.handletime = date.dateFn(v.handletime);
                });
                this.userlist = res.data.data;
                console.log(this.userlist);
            });
        },
        // 删除信息
        open(data) {
            this.$confirm("此操作将永久删除该信息, 是否继续?", "提示", {
                confirmButtonText: "确定",
                cancelButtonText: "取消",
                type: "warning",
            }).then(() => {
                this.del(data);
            });
        },
        // 删除信息的方法
        del(data) {
            // console.log(data);
            this.$axios({
                url: "/orders/delorder",
                method: "post",
                data: data,
            }).then((res) => {
                if (res.data.code == 200) {
                    this.getUserList();
                } else {
                    this.$message({
                        message: "无法删除",
                        tyep: "warning",
                    });
                }
            });
        },
        // 筛选方法信息
        Screening() {
            this.nowPage = 1;
            this.$axios({
                url: "/orders/getChaXun",
                params: {
                    phone: this.formInline.phone,
                    search: this.formInline.search,
                    time: this.formInline.select,
                },
            }).then((res) => {
                res.data.data.filter((v) => {
                    if (v.subtime) {
                        v.subtime = date.dateFn(v.subtime);
                    }
                    if (v.handletime) {
                        v.handletime = date.dateFn(v.handletime);
                    }
                });
                console.log(res.data);
                if (res.data.code == 200) {
                    this.userlist = res.data.data;
                }
            });
        },
        // 重置
        Reset() {
            this.nowPage = 1;
            this.formInline.phone = "";
            (this.formInline.search = ""),
                (this.formInline.select = ""),
                this.getUserList();
        },
        // 刷新
        Newpage() {
            this.$message({
                message: "已刷新",
                type: "success",
            });
        },
        editClose() {
            this.CheckDialogVisible = false;
        },
        // 编辑数据
        handleEdit(data) {
            this.CheckDialogVisible = true;
            this.editTableData = data;
        },
        onEditOk() {
            // console.log(this.editTableData);
            this.$axios({
                url: "/ModifyData",
                data: {
                    id: this.editTableData.id,
                    ordernum: this.editTableData.ordernum,
                    subtime: date.dateFn(this.editTableData.subtime),
                    phone: this.editTableData.phone,
                    money: this.editTableData.money,
                    type: this.editTableData.type,
                },
                method: "post",
            }).then((res) => {
                this.editTableData = res.data.data;
                // console.log(res);
                if (res.data.code == 200) {
                    this.$message("修改成功", "success");
                    this.getuser();
                } else {
                    this.$message("失败", "error");
                }
            });
            this.CheckDialogVisible = false;
        },
        //监听pagesize改变的事件
        handleSizeChange(newSize) {
            console.log(newSize);
            this.eachNum = newSize;
            this.getUserList();
        },
        // 监听页码值的改变的事件
        handleCurrentChange(newPage) {
            this.nowPage = newPage;
            this.getUserList();
        },
    },
    // 计算分页
    computed: {
        tableData() {
            return this.userlist.slice(
                (this.nowPage - 1) * this.eachNum,
                this.nowPage * this.eachNum
            );
        },
    },
};
</script>
<style lang="less" scoped>
// 头部刷新区域
.setdiv {
    background-color: #f2f2f2;
    padding: 17.5px 0;
    position: relative;
}
.box_card {
    margin-top: 10px;
}
.setdiv > span {
    margin-left: 20px;
}
.RERight {
    position: absolute;
    right: 20px;
    top: 8px;
}
.anniu {
    width: 100px;
    height: 40px;
}
.input {
    width: 200px;
    margin-left: 20px;
    margin-right: 50px;
}
#anniu1 {
    text-align: center;
}
.Tdata {
    margin-top: 20px;
}
#fenye {
    margin-top: 10px;
    text-align: center;
}
// 卡片头部变色
.el-card /deep/ .el-card__header {
    background-color: #f2f2f2;
}
/deep/.el-collapse-item__header {
    padding-left: 18px;
    font-size: 16px;
    height: 56px;
    background-color: rgb(242, 242, 242);
}
/deep/.el-collapse-item__content {
    margin-top: 40px;
}
</style>